package com.xh.web.util;

import com.xh.web.constant.DbModuleConstants;
import com.xh.web.domain.DDbConnect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbUtil {

    public static Connection getConn(DDbConnect db) throws SQLException, ClassNotFoundException {
        String dbUrl="";
        if(db.getDbType().equals("mysql")){
            Class.forName("com.mysql.cj.jdbc.Driver");
            dbUrl="jdbc:mysql://"+db.getDbUrl()+":"+db.getDbPort() +"/"+db.getDbName()
            +"?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        }else if(db.getDbType().equals("oracle")){
            Class.forName("oracle.jdbc.driver.OracleDriver");
            dbUrl="jdbc:oracle:thin:@"+db.getDbUrl()+":"+db.getDbPort()+":"+db.getDbName();
        }else if(db.getDbType().equals("dm")){
            Class.forName("dm.jdbc.driver.DmDriver");
            dbUrl="jdbc:dm://"+db.getDbUrl()+":"+db.getDbPort();
        }
        Connection conn=DriverManager.getConnection(dbUrl,db.getUserName(),db.getPassword());
        return conn;
    }

    public static void close(Connection conn) throws SQLException {
        if (conn != null) {
            conn.close();
        }

    }

    public static void closeRs(ResultSet rs) throws SQLException {
        if (rs != null) {
            rs.close();
        }

    }

    public static boolean isGetLength(String dbType,String columnType){
        boolean isTrue=false;
        String temp= DbModuleConstants.dataType1.get(columnType.toLowerCase());
        if(temp!=null){
            return true;
        }
        return isTrue;
    }
}
